import Vue from 'vue'
import Router from 'vue-router'
import Home from './views/Home.vue'
import Cms from './views/Cms.vue'
import { Notification } from "element-ui";
Vue.use(Router)


//官网页面
import Index from '@/views/HomePage/Index.vue'
import appServer from '@/views/HomePage/Server.vue'
import About from '@/views/HomePage/AboutUs.vue'
import Industry from '@/views/HomePage/Industry.vue'
import Area from '@/views/HomePage/Area.vue'
import Resolve from '@/views/HomePage/Resolve.vue'
import AppDetails from '@/views/HomePage/AppDetails.vue'
import AdvisoryCooperation from '@/views/HomePage/AdvisoryCooperation.vue'
// 官网路由
const HomePage = [
  {
    path: '/home',
    name: 'home',
    redirect: '/home/index',
    component: Home,
    children: [
      {
        path: '/home/index',
        name: 'home_index',
        component: Index
      },
      {
        path: '/home/server',
        name: 'home_server',
        component: appServer
      },

      {
        path: '/home/about',
        name: 'about_us',
        component: About
      },
      {
        path: '/home/industry',
        name: 'home_industry',
        component: Industry
      },
      {
        path: '/home/area',
        name: 'home_area',
        component: Area
      },
      {
        path: '/home/resolve',
        name: 'home_resolve',
        component: Resolve
      },
      {
        path: '/home/appDetails',
        name: 'app_details',
        component: AppDetails
      },
      {
        path: '/home/advisoryCooperation',
        name: 'advisory_cooperation',
        component: AdvisoryCooperation
      },
    ]
  },

]

// 控制台路由
const CmsPage = [
  {
    path: '/cms',
    name: 'cms',
    component: Cms,
    redirect: '/cms/index',
    children: [
      {
        icon: 'el-icon-s-home',
        meta: {
          title: '控制台首页',
        },
        path: '/cms/index',
        name: 'cms_index',
        component: () => import(/* webpackChunkName: "cms_index" */ '@/views/CmsPage/Index.vue')
      },
      {
        icon: 'el-icon-s-custom',
        path: '/cms/user',
        name: "cms_user",
        meta: {
          title: '个人中心',
        },
        redirect: '/cms/user/basic',
        component: { render: h => h("router-view") },  // 子路由需要添加
        children: [
          {
            meta: {
              title: '基本信息',
            },
            name: "cms_basic",
            path: '/cms/user/basic',
            component: () => import(/* webpackChunkName: "cms_basic" */ '@/views/CmsPage/UserCenter/Basic.vue')
          },
          {
            meta: {
              title: '企业认证',
            },
            name: "cms_enterprise",
            path: '/cms/user/enterprise',
            component: () => import(/* webpackChunkName: "cms_enterprise" */ '@/views/CmsPage/UserCenter/EnterPrise.vue')
          },
          {
            meta: {
              title: '开发者认证',
            },
            name: "cms_developer",
            path: '/cms/user/developer',
            component: () => import(/* webpackChunkName: "cms_developer" */ '@/views/CmsPage/UserCenter/Developer.vue')
          },
          {
            meta: {
              title: '消息中心',
            },
            name: "msg_center",
            path: '/cms/user/msg',
            component: () => import(/* webpackChunkName: "cms_msg" */ '@/views/CmsPage/UserCenter/MsgCenter.vue')
          },

        ]
      },
      {
        icon: 'el-icon-notebook-1',
        path: '/cms/account_ctrl',
        name: "cms_account",
        meta: {
          title: '企业账户管理',
        },
        redirect: '/cms/account_ctrl/user_ctrl',
        component: { render: h => h("router-view") },  // 子路由需要添加
        children: [
          {
            meta: {
              title: '用户管理',
            },
            name: "user_ctrl",
            path: '/cms/account_ctrl/user_ctrl',
            component: () => import(/* webpackChunkName: "user_ctrl" */ '@/views/CmsPage/AccountCtrl/UserCtrl.vue')
          },
          {
            meta: {
              title: '用户组管理',
            },
            name: "user_group_ctrl",
            path: '/cms/account_ctrl/user_group_ctrl',
            component: () => import(/* webpackChunkName: "user_group_ctrl" */ '@/views/CmsPage/AccountCtrl/UserGroupCtrl.vue')
          },

        ]
      },
      {
        icon: 'el-icon-menu',
        path: '/cms/app',
        name: "cms_app",
        meta: {
          title: '应用中心',
        },
        redirect: '/cms/app/app_ctrl',
        component: { render: h => h("router-view") },  // 子路由需要添加
        children: [
          {
            meta: {
              title: '应用管理',
            },
            name: "app_ctrl",
            path: '/cms/app/app_ctrl',
            component: () => import(/* webpackChunkName: "app_ctrl" */ '@/views/CmsPage/AppCenter/AppCtrl.vue')
          },
          {
            meta: {
              title: '我的应用',
            },
            name: "my_app",
            path: '/cms/app/my_app',
            component: () => import(/* webpackChunkName: "my_app" */ '@/views/CmsPage/AppCenter/MyApp.vue')
          },
          {
            meta: {
              title: '我的文件',
            },
            name: "my_files",
            path: '/cms/app/my_files',
            component: () => import(/* webpackChunkName: "my_files" */ '@/views/CmsPage/AppCenter/MyFiles.vue')
          },

        ]
      },

    ]
  }
]


// homepage cmspage 位置请不要改变
const router = new Router({
  routes: [
    {
      path: '/',
      redirect: '/home'
    },
    ...HomePage,
    ...CmsPage,
    {
      path: '/login',
      component: () => import(/* webpackChunkName: "login" */ '@/views/Login.vue')
    },
    {
      path: "*",
      component: () => import(/* webpackChunkName: "not_found" */ '@/views/404.vue')
    }
  ]
})


// 路由跳转白名单
// const no_need_token = [
//   '/login',
//   '/home/index'
// ]

// 路由钩子函数
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('user_token');
  if (token) {
    //有令牌
    if (to.path === '/login') {
      next({ path: '/home/index' })
    } else {
      next()
    }
  } else {

    //无令牌
    if (to.path.includes('/home') || to.path.includes('/login')) {
      next()
    } else {
      Notification.error({
        title: '提示',
        message: '请您先登陆',
        duration: 3000
      });
      next({ path: login_page })
    }
  }
})

router.afterEach((to, from) => {
  // NProgress.done()
})


export default router

